package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"group-card/common/constant"
	"group-card/common/request"
	"group-card/model/entity"
	"group-card/rpc/card/cardclient"
	"group-card/rpc/card/internal/svc"
)

type SkinListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSkinListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SkinListLogic {
	return &SkinListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SkinListLogic) SkinList(in *cardclient.SkinListRequest) (*cardclient.SkinListResponse, error) {
	var ctx context.Context

	req := &entity.CardSkinReq{
		Name:      in.Name,
		FaceValue: in.FaceValue,
		Type:      in.Type,
		BaseReq: request.BaseReq{
			PageNum:  in.PageNum,
			PageSize: in.PageSize,
		},
	}
	data, count, err := l.svcCtx.SkinModel.List(ctx, req)
	if err != nil {
		return nil, err
	}

	var list []*cardclient.SkinListData
	for _, value := range data {
		list = append(list, &cardclient.SkinListData{
			Id:         value.Id,
			Name:       value.Name,
			FaceValue:  value.FaceValue,
			Colour:     value.Colour,
			CustomerId: value.CustomerId,
			Type:       value.Type,
			Status:     value.Status,
			Image:      value.Image,
			AdminId:    value.AdminId,
			CreateAt:   value.CreateAt.Format(constant.YMDHIS),
			UpdateAt:   value.UpdateAt.Format(constant.YMDHIS),
			DeleteAt:   value.DeleteAt.Format(constant.YMDHIS),
		},
		)
	}

	return &cardclient.SkinListResponse{
		Total: count,
		List:  list,
	}, nil

}
